<?php
/**
 * @特惠赚、评论控制器
 * Author: 小谢     
 * Date: 2019/05/11
 */
namespace app\admin\controller;
use think\facade\Session;
use think\facade\View;
use think\Db;

class Comment extends Base {
  
    /**
     * @brief 评论列表
     */
    public function lists() {
        $p = I('p/d',1);
        $p = I('p/d',1);
        $where['parent_id'] = 0;
        $list = M('comment')->alias('c')->join('__GOODS__ g','c.goods_id = g.goods_id')->where($where)->order('sort ASC')->page($p.',20')->paginate(20);
        $count = $list->count();
        View::share(['list'=>$list,'count'=>$count]);
        return View();
    }

    /**
     * @brief 查看评论
     */
    public function detail() {
        $id = I('get.id/d');
        $res = Db::name('comment')->where(['comment_id'=>$id])->find();

        if(!$res){
            exit($this->error('不存在该评论'));
        }

        if(I('post.')){
            $admin_id = session('admin_id');
            $admin_name = Db::name('admin')->where(['admin_id'=>$admin_id])->getField('user_name');
            $add['parent_id'] = $id;
            $add['content'] = trim(I('post.content'));
            $add['goods_id'] = $res['goods_id'];
            $add['add_time'] = time();
            $add['username'] = $admin_name;
            $add['is_show'] = 1;
            empty($add['content']) && $this->error('请填写回复内容');
            $row =  M('comment')->add($add);
            if($row){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
            exit;
        }
        $reply = M('comment')->where(array('parent_id'=>$id))->select(); // 评论回复列表
        $res['img'] = json_decode($res['img']);
        View::share(['comment'=>$res,'reply'=>$reply]);
        return View();
    }

    /**
     * @brief 删除评论
     */
    public function del() {
        $id = I('post.id');
        $res = M('comment')->where('comment_id', 'IN', $id)->whereOr('parent_id', 'IN', $id)->delete();
        if($res !== false){
            $this->ajaxReturn(['status' => 1,'msg' => '操作完成','url'=>U('Admin/Comment/index')]);
        }else{
            $this->ajaxReturn(['status' => -1,'msg' => '操作失败','url'=>U('Admin/Comment/index')]);
        }
    }
    
}